package org.icress.fusion.admin

class RecordTypeController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    
    def recordTypeService
    
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 100, 100)
        render view : '/admin/recordType/list', model : [recordTypeInstanceList: RecordType.list(params), recordTypeInstanceTotal: RecordType.count()]
    }

    def create = {
        def recordTypeInstance = new RecordType()
        recordTypeInstance.properties = params
        
        def domainList = recordTypeService.domainList(grailsApplication)
        
        render view : '/admin/recordType/create', model : [recordTypeInstance: recordTypeInstance, domainList : domainList]
    }

    def save = {
        def recordTypeInstance = new RecordType(params)
        recordTypeInstance.validate()
        if (!recordTypeInstance.hasErrors() && recordTypeInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'admin.recordType.label', default: 'RecordType'), recordTypeInstance.id])}"
            redirect(action: "show", id: recordTypeInstance.id)
        }else{
            render(view: "/admin/recordType/create", model: [recordTypeInstance: recordTypeInstance])
        }
    }

    def show = {
        def recordTypeInstance = RecordType.get(params.id)
        if (!recordTypeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.recordType.label', default: 'RecordType'), params.id])}"
            redirect(action: "list")
        }else {
            render view : '/admin/recordType/show', model : [recordTypeInstance: recordTypeInstance]
        }
    }

    def edit = {
        def recordTypeInstance = RecordType.get(params.id)
        if (!recordTypeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.recordType.label', default: 'RecordType'), params.id])}"
            redirect(action: "list")
        }else {
            def domainList = recordTypeService.domainList(grailsApplication)
            render view : '/admin/recordType/edit', model : [recordTypeInstance: recordTypeInstance, domainList : domainList]
        }
    }
    
    def update = {
        def recordTypeInstance = RecordType.get(params.id)
        if (recordTypeInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (recordTypeInstance.version > version) {
                    
                    recordTypeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'admin.recordType.label', default: 'RecordType')] as Object[], "Another user has updated this RecordType while you were editing")
                    def domainList = recordTypeService.domainList(grailsApplication)
                    render view : '/admin/recordType/edit', model : [recordTypeInstance: recordTypeInstance, domainList : domainList]
                    return
                }
            }
            recordTypeInstance.properties = params
            recordTypeInstance.validate()
            if (!recordTypeInstance.hasErrors() && recordTypeInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'admin.recordType.label', default: 'RecordType'), recordTypeInstance.id])}"
                redirect(action: "show", id: recordTypeInstance.id)
            }else {
                def domainList = recordTypeService.domainList(grailsApplication)
                render view : '/admin/recordType/edit', model : [recordTypeInstance: recordTypeInstance, domainList : domainList]
            }
        } else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.recordType.label', default: 'RecordType'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def recordTypeInstance = RecordType.get(params.id)
        if (recordTypeInstance) {
            try {
                recordTypeInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'admin.recordType.label', default: 'RecordType'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'admin.recordType.label', default: 'RecordType'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.recordType.label', default: 'RecordType'), params.id])}"
            redirect(action: "list")
        }
    }
}